package org.shoukaiseki.aliyun.oss.config;

import com.aliyun.oss.OSSClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@EnableConfigurationProperties({AliyunOSSCoreProperties.class,BaseOSSPathProperties.class})
public class AliyunOSSCoreAutoConfig {

    @Resource
    AliyunOSSCoreProperties aliyunOSSCoreProperties;

    /**
     * 线程安全对象,单利模式即可,不需要 shutdown
     */
    @Bean("ossClient")
    public OSSClient ossClient(){
        return new OSSClient(aliyunOSSCoreProperties.getEndpoint(),aliyunOSSCoreProperties.getAccessKeyId(),aliyunOSSCoreProperties.getSecretAccessKey());
    }


    /**
     * 外网访问url生成
     * @return
     */
    @Bean("urlMakeOSSClient")
    public OSSClient urlMakeOSSClient(){
        String endpoint = aliyunOSSCoreProperties.getEndpoint();
        if(endpoint.indexOf("-internal")>-1){
            endpoint=endpoint.replace("-internal","");
        }
        return new OSSClient(endpoint,aliyunOSSCoreProperties.getAccessKeyId(),aliyunOSSCoreProperties.getSecretAccessKey());
    }
}
